package bysj.dao;


import bysj.domain.GraduateProjectCategory;
import bysj.domain.GraduateProjectStatus;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.TreeSet;

public final class GraduateProjectStatusDao {
    private static GraduateProjectStatusDao graduateProjectStatusDao = new GraduateProjectStatusDao();
    private static Collection<GraduateProjectStatus> graduateProjectStatuses;

//    static {
//        graduateProjectStatuses = new TreeSet<GraduateProjectStatus>();
//        GraduateProjectStatus graduateProjectStatus = new GraduateProjectStatus(1, "申请", "01", "");
//        graduateProjectStatuses.add(graduateProjectStatus);
//        graduateProjectStatuses.add(new GraduateProjectStatus(2, "通过", "02", ""));
//        graduateProjectStatuses.add(new GraduateProjectStatus(3, "退修", "03", ""));
//    }

    private GraduateProjectStatusDao() {
    }

    public static GraduateProjectStatusDao getInstance() {
        return graduateProjectStatusDao;
    }

    public Collection<GraduateProjectStatus> findAll() {
        return GraduateProjectStatusDao.graduateProjectStatuses;
    }

    public GraduateProjectStatus find(Integer id, Connection connection) throws SQLException {
        GraduateProjectStatus desiredGraduateProjectStatus = null;
        String sql_select_id="SELECT*FROM GraduateProjectStatus WHERE id=?";
        PreparedStatement pstmt = connection.prepareStatement(sql_select_id);
        pstmt.setInt(1,id);
        ResultSet resultSet=pstmt.executeQuery();
        if(resultSet.next()){
            String description=resultSet.getString("description");
            String no = resultSet.getString("no");
            String remarks = resultSet.getString("remarks");
            desiredGraduateProjectStatus= new GraduateProjectStatus(id,description,no,remarks);
        }
        JdbcHelper.close(resultSet,pstmt);
        return desiredGraduateProjectStatus;
    }

    public boolean update(GraduateProjectStatus graduateProjectStatus) {
        graduateProjectStatuses.remove(graduateProjectStatus);
        return graduateProjectStatuses.add(graduateProjectStatus);
    }

    public boolean add(GraduateProjectStatus graduateProjectStatus) {
        return graduateProjectStatuses.add(graduateProjectStatus);
    }

//    public boolean delete(Integer id) {
//        GraduateProjectStatus graduateProjectStatus = this.find(id);
//        return this.delete(graduateProjectStatus);
//    }

    public boolean delete(GraduateProjectStatus graduateProjectStatus) {
        return GraduateProjectStatusDao.graduateProjectStatuses.remove(graduateProjectStatus);
    }
}
